package cn.iocoder.yudao.module.student.service.invite;

import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.member.api.company.CompanyApi;
import cn.iocoder.yudao.module.member.api.company.dto.CompanyRespDTO;
import cn.iocoder.yudao.module.member.api.curriculumVitaeSendRecord.CurriculumVitaeSendRecordApi;
import cn.iocoder.yudao.module.member.api.curriculumVitaeSendRecord.dto.CurriculumVitaeSendRecordRespDTO;
import cn.iocoder.yudao.module.member.api.curriculumVitaeSendRecord.dto.CurriculumVitaeSendRecordRespJobDTO;
import cn.iocoder.yudao.module.member.api.curriculumVitaeSendRecord.dto.CurriculumVitaeSendRecordUpdateReqDTO;
import cn.iocoder.yudao.module.member.api.job.JobApi;
import cn.iocoder.yudao.module.member.api.job.dto.JobRespDTO;
import cn.iocoder.yudao.module.system.api.message.MessageApi;
import cn.iocoder.yudao.module.system.api.message.dto.MessageCreateReqDTO;
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.student.controller.app.invite.vo.*;
import cn.iocoder.yudao.module.student.dal.dataobject.invite.InviteDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.student.convert.invite.InviteConvert;
import cn.iocoder.yudao.module.student.dal.mysql.invite.InviteMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.student.enums.ErrorCodeConstants.*;

/**
 * 面试邀约 Service 实现类
 *
 * @author 周枫皓
 */
@Service
@Validated
public class InviteServiceImpl implements InviteService {

    @Resource
    private InviteMapper inviteMapper;
    @Resource
    private CurriculumVitaeSendRecordApi curriculumVitaeSendRecordApi;
    @Resource
    private JobApi jobApi;
    @Resource
    private MessageApi messageApi;
    @Resource
    private CompanyApi companyApi;
    @Resource
    private AdminUserApi adminUserApi;

    @Override
    public Long createInvite(AppInviteCreateReqVO createReqVO) {
        // 插入
        InviteDO invite = InviteConvert.INSTANCE.convert(createReqVO);
        inviteMapper.insert(invite);
        // 返回
        return invite.getId();
    }

    @Override
    public void updateInvite(AppInviteUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateInviteExists(updateReqVO.getId());
        // 更新
        InviteDO updateObj = InviteConvert.INSTANCE.convert(updateReqVO);
//        InviteDO inviteDo=this.getInvite(updateObj.getId());
        CurriculumVitaeSendRecordUpdateReqDTO curriculumVitaeSendRecordUpdateReqDTO=new CurriculumVitaeSendRecordUpdateReqDTO();
        curriculumVitaeSendRecordUpdateReqDTO.setId(updateReqVO.getSendRecordId());
        curriculumVitaeSendRecordUpdateReqDTO.setStatus(updateObj.getStatus());
        curriculumVitaeSendRecordApi.updateCurriculumVitaeSend(curriculumVitaeSendRecordUpdateReqDTO);
        inviteMapper.updateById(updateObj);
        String title;
        String message;
        CurriculumVitaeSendRecordRespDTO curriculumVitaeSendRecordRespDTO=curriculumVitaeSendRecordApi.getCurriculumVitaeSend(updateReqVO.getSendRecordId(),false);
        Map<String, Long> phones=jobApi.getCompanyAdminCreatorPhone(updateReqVO.getJobId());
        phones.put("pushUser",curriculumVitaeSendRecordRespDTO.getPublisherId());
        if(curriculumVitaeSendRecordUpdateReqDTO.getStatus().equals(5)){
            title=curriculumVitaeSendRecordUpdateReqDTO.getJobName()+"：确认面试邀约 -"+curriculumVitaeSendRecordUpdateReqDTO.getStudentName();
            message="您好，收到"+curriculumVitaeSendRecordUpdateReqDTO.getStudentName()+"针对"+curriculumVitaeSendRecordUpdateReqDTO.getJobName()+"职位的面试邀约确认，请注意查收。";
        }else {
            title=curriculumVitaeSendRecordUpdateReqDTO.getJobName()+"：拒绝面试邀约 -"+curriculumVitaeSendRecordUpdateReqDTO.getStudentName();
            message="您好，收到"+curriculumVitaeSendRecordUpdateReqDTO.getStudentName()+"针对"+curriculumVitaeSendRecordUpdateReqDTO.getJobName()+"职位的面试邀约拒绝，请注意查收。";
        }
        for(String key:phones.keySet()){
            Long id = phones.get(key);
            sendmessage(1l,id,title,message);
        }
    }
    @Async
    void sendmessage(Long type, Long id, String title, String messageText){
        MessageCreateReqDTO messageCreateReqVO=new MessageCreateReqDTO();
        messageCreateReqVO.setPhone("");
        messageCreateReqVO.setType(type);
        messageCreateReqVO.setStatus("5");
        messageCreateReqVO.setUserId(id);
        messageCreateReqVO.setStudentName("系统管理员");
        messageCreateReqVO.setRemark(messageText);
        messageCreateReqVO.setTitle(title);
        messageApi.sendMessage(messageCreateReqVO);
    }
    @Override
    public void deleteInvite(Long id) {
        // 校验存在
        this.validateInviteExists(id);
        // 删除
        inviteMapper.deleteById(id);
    }

    private void validateInviteExists(Long id) {
        if (inviteMapper.selectById(id) == null) {
            throw exception(INVITE_NOT_EXISTS);
        }
    }

    @Override
    public InviteDO getInvite(Long id) {
        InviteDO inviteDO=inviteMapper.selectById(id);
        if(inviteDO!=null&&inviteDO.getStatus().equals("2")) {
            inviteDO.setId(id);
            inviteDO.setStatus("3");
            inviteMapper.updateById(inviteDO);
            CurriculumVitaeSendRecordUpdateReqDTO updateReqDTO=new CurriculumVitaeSendRecordUpdateReqDTO();
            updateReqDTO.setId(inviteDO.getSendRecordId());
            updateReqDTO.setStatus("3");
            curriculumVitaeSendRecordApi.updateCurriculumVitaeSendRecord(updateReqDTO);

        }
        if(inviteDO!=null&&inviteDO.getStatus().equals("5")) {
            inviteDO.setId(id);
            inviteDO.setShowStatus("1");
            inviteMapper.updateById(inviteDO);
        }
        return inviteDO;
    }

    @Override
    public List<InviteDO> getInviteList(Collection<Long> ids) {
        return inviteMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<AppInviteRespVO> getInvitePage(AppInvitePageReqVO pageReqVO) {
        LoginUser loginUser= SecurityFrameworkUtils.getLoginUser();
        pageReqVO.setStudentId(loginUser.getId());
        PageResult<InviteDO> inviteDOPageResult= inviteMapper.selectPage(pageReqVO);
        PageResult<AppInviteRespVO>  appInviteRespVOPageResult=InviteConvert.INSTANCE.convertPage(inviteDOPageResult);
        List<AppInviteRespVO> inviteDOList=appInviteRespVOPageResult.getList();
        for (AppInviteRespVO inviteDO:inviteDOList) {
            CurriculumVitaeSendRecordRespDTO curriculumVitaeSendRecordRespDTO = curriculumVitaeSendRecordApi.getCurriculumVitaeSend(inviteDO.getSendRecordId(),false);
            inviteDO.setCurriculumVitaeSendRecordRespDTO(curriculumVitaeSendRecordRespDTO);
        }
        appInviteRespVOPageResult.setList(inviteDOList);
        return appInviteRespVOPageResult;
    }

    @Override
    public PageResult<AppInviteRespVO> invitedFeedback(AppInvitePageReqVO pageReqVO) {
        LoginUser loginUser= SecurityFrameworkUtils.getLoginUser();
        pageReqVO.setStudentId(loginUser.getId());
        List<String> list=new ArrayList<String>();
        list.add("5");
        list.add("6");
        list.add("7");
        list.add("8");
        PageResult<InviteDO> inviteDOPageResult= inviteMapper.selectPage(pageReqVO,list);
        PageResult<AppInviteRespVO>  appInviteRespVOPageResult=InviteConvert.INSTANCE.convertPage(inviteDOPageResult);
        List<AppInviteRespVO> inviteDOList=appInviteRespVOPageResult.getList();
        for (AppInviteRespVO inviteDO:inviteDOList) {
            CurriculumVitaeSendRecordRespDTO curriculumVitaeSendRecordRespDTO = curriculumVitaeSendRecordApi.getCurriculumVitaeSend(inviteDO.getSendRecordId(),false);
            inviteDO.setCurriculumVitaeSendRecordRespDTO(curriculumVitaeSendRecordRespDTO);
        }
        appInviteRespVOPageResult.setList(inviteDOList);
        return appInviteRespVOPageResult;
    }

    @Override
    public List<InviteDO> getInviteList(AppInviteExportReqVO exportReqVO) {
        return inviteMapper.selectList(exportReqVO);
    }

}
